package controller

import (
	"github.com/gin-gonic/gin"
	"learngin/model"
	"learngin/param"
	"learngin/service"
)

//获取用户信息
func MemberInfo(c *gin.Context) {
	userId := c.GetInt("userId")
	token := c.GetHeader("token")
	var member model.Member
	member.Token = token
	memberErr := model.DB.Table("member").Where("id = ?", userId).Find(&member).Error
	if memberErr != nil {
		service.Error(c, "用户信息查询失败", memberErr.Error())
		return
	}
	service.Success(c, member)
}

//账号密码登录
func MemberAccountLogin(c *gin.Context) {
	MemberLoginParam := param.MemberLoginParam{}
	var member model.Member
	_ = c.BindJSON(&MemberLoginParam)
	result := model.MemberAccountLogin(MemberLoginParam.Account, MemberLoginParam.Password)
	if !result {
		service.Error(c, "账号或者密码不正确", nil)
		return
	}
	token := service.SetTingToken(member.Id)
	data := make(map[string]string) //初始化map
	data["token"] = token
	service.Success(c, data)
}

//手机号登录
func MemberMobileLogin(c *gin.Context) {
	//var MemberMobileLoginParam param.MemberMobileLoginParam{}

}

//获取短信验证码
func MemberGetMobileCode(c *gin.Context) {
	var MemberGetMsgCodeParam param.MemberGetMsgCodeParam
	_ = c.BindJSON(&MemberGetMsgCodeParam)
	numResult := model.SendMsgImposeNum(MemberGetMsgCodeParam.Mobile)
	if !numResult {
		service.Error(c, "发送短信次数达到上线", nil)
		return
	}
	result, msg := service.SendCode(MemberGetMsgCodeParam.Mobile, MemberGetMsgCodeParam.Status)
	if !result {
		service.Error(c, msg, nil)
		return
	}
	service.Success(c, nil)
}
